﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
//using System.Collections;
using System.Xml;

public partial class Comod_StorageManage : ATPages
{
    Function f = new Function();
    ZSql zs = new ZSql();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            HFroid.Value = Request.Cookies["user"].Values["roleid"];//角色22:售后服务部 
            hfxs.Value = Request.QueryString["moduid"].ToString();
            lblPath.Text = f.getModuleidAdd(Request.QueryString["moduid"]);//当前位置
            yjTm1.Text = Request.QueryString["yjTm1"];
            yjTm2.Text = Request.QueryString["yjTm2"];
            Spec.Text = Request.QueryString["Spec"];
            catalogueName.Text = Request.QueryString["catalogueName"];
            ComodBarcode.Text = Request.QueryString["ComodBarcode"];
            TypeName.Text = Request.QueryString["TypeName"];
            AuditState.SelectedValue = Request.QueryString["AuditState"];
            ProcNum.SelectedValue = Request.QueryString["ProcNum"];
            initGvData();//邦定数据  
        }
    }
    private void initGvData()//初使化数据
    {
        string search = "";
        string sql = "SELECT * FROM (SELECT a.*, (SELECT WarehouseName FROM SV_Warehouse   WHERE WarehouseId = a.WarehouseId) AS WarehouseName,(select TypeName from SV_Type where TypeId=a.TypeId) as TypeName FROM SV_Stock a ) Stock where 1=1 ";
        if (!string.IsNullOrEmpty(Request.QueryString["yjTm1"]) && !string.IsNullOrEmpty(Request.QueryString["yjTm2"]))
        {
            search += "&yjTm1=" + yjTm1.Text.Trim();
            search += "&yjTm2=" + yjTm2.Text.Trim();
            sql += " and storageTime>='" + Request.QueryString["yjTm1"] + "' and storageTime<='" + Request.QueryString["yjTm2"] + "' ";
        }
        if (!string.IsNullOrEmpty(Request.QueryString["Spec"]))
        {
            search += "&Spec=" + Common.FormatParameter(Spec.Text.Trim());
            sql += " and Spec like'%" + Request.QueryString["Spec"] + "%' ";
        }
        if (!string.IsNullOrEmpty(Request.QueryString["catalogueName"]))
        {
            search += "&catalogueName=" + Common.FormatParameter(catalogueName.Text.Trim());
            sql += " and catalogueName like'%" + Request.QueryString["catalogueName"] + "%' ";
        }
        if (!string.IsNullOrEmpty(Request.QueryString["ComodBarcode"]))
        {
            search += "&ComodBarcode=" + Common.FormatParameter(ComodBarcode.Text.Trim());
            sql += " and ComodBarcode like'%" + Request.QueryString["ComodBarcode"] + "%' ";
        }
        if (!string.IsNullOrEmpty(Request.QueryString["TypeName"]))
        {
            search += "&TypeName=" + Common.FormatParameter(TypeName.Text.Trim());
            sql += " and TypeName like'%" + Request.QueryString["TypeName"] + "%'";

        }
        if (!string.IsNullOrEmpty(Request.QueryString["AuditState"]))
        {
            search += "&AuditState=" + Common.FormatParameter(AuditState.SelectedValue.Trim());
            sql += " and AuditState ='" + Request.QueryString["AuditState"] + "' ";
        }
        if (!string.IsNullOrEmpty(Request.QueryString["ProcNum"]))
        {
            search += "&ProcNum=" + Request.QueryString["ProcNum"];
            sql += "  and ProcNum" + Request.QueryString["ProcNum"];
        }
        sql += " ORDER BY stockId DESC";

        //Response.Write(sql);
        //Response.End();
        PageNavigator1.Url = "../Services/StorageManage.aspx?moduid=" + hfxs.Value + search;
        PageNavigator1.Total = 1;
        PageNavigator1.Query = sql;

    }
    protected void linkbtnUpdate_Click(object sender, EventArgs e)//修改
    {
        string id = (sender as LinkButton).CommandArgument;
        Response.Redirect("StorageAdd.aspx?id=" + id + "&action=update&moduid=" + Request.QueryString["moduid"]);
    }

    protected void lnkbtnFlag_Click(object sender, EventArgs e)//审核
    {
        string id = (sender as LinkButton).CommandArgument;
        Response.Redirect("StorageAdd.aspx?id=" + id + "&action=flag&moduid=" + Request.QueryString["moduid"]);
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string search = "";
        if (!string.IsNullOrEmpty(yjTm1.Text) && !string.IsNullOrEmpty(yjTm2.Text.Trim()))
        {
            search += "&yjTm1=" + yjTm1.Text.Trim();
            search += "&yjTm2=" + yjTm2.Text.Trim();
        }
        if (!string.IsNullOrEmpty(Spec.Text.Trim()))
        {
            search += "&Spec=" + Common.FormatParameter(Spec.Text.Trim());
        }
        if (!string.IsNullOrEmpty(catalogueName.Text.Trim()))
        {
            search += "&catalogueName=" + catalogueName.Text.Trim();
        }
        if (!string.IsNullOrEmpty(ComodBarcode.Text))
        {
            search += "&ComodBarcode=" + Common.FormatParameter(ComodBarcode.Text.Trim());
        }
        if (!string.IsNullOrEmpty(TypeName.Text.Trim()))
        {
            search += "&TypeName=" + Common.FormatParameter(TypeName.Text.Trim());
        }
        if (!string.IsNullOrEmpty(AuditState.SelectedValue.Trim()))
        {
            search += "&AuditState=" + Common.FormatParameter(AuditState.SelectedValue.Trim());
        }
        if (!string.IsNullOrEmpty(ProcNum.Text))//库存
        {
            search += "&ProcNum=" + Common.FormatParameter(ProcNum.Text);
        }
        PageNavigator1.Url = "../Services/StorageManage.aspx?moduid=" + hfxs.Value + search;
        Response.Redirect("StorageManage.aspx?moduid=" + hfxs.Value + search);
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("StorageAdd.aspx?moduid=" + Request["moduid"] + "&action=add");
    }
    protected void btnDel_Click(object sender, EventArgs e)//删除操作
    {
        int n = 0;
        if (this.GvData.Rows.Count > 0)
        {
            for (int i = 0; i < GvData.Rows.Count; i++)
            {
                CheckBox ck = GvData.Rows[i].FindControl("ckFlag") as CheckBox;
                if (ck.Checked)
                {
                    string id = this.GvData.Rows[i].Cells[0].Text;
                    zs.Open("delete SV_Stock  where StockId=" + id);
                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('删除成功');", true);
                }
                else
                {
                    n++;
                }
                if (n == this.GvData.Rows.Count)
                {

                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('至少应选择一项')", true);
                }
            }
        }
        zs.Close();
    }

    protected void btnallflag_Click(object sender, EventArgs e)//审核操作
    {
        int n = 0;
        if (this.GvData.Rows.Count > 0)
        {
            for (int i = 0; i < GvData.Rows.Count; i++)
            {
                CheckBox ck = GvData.Rows[i].FindControl("ckFlag") as CheckBox;
                if (ck.Checked)
                {
                    string id = this.GvData.Rows[i].Cells[0].Text;
                    zs.Open("Update  SV_Stock set AuditState=1,AuditUserName='" + Server.UrlDecode(Request.Cookies["user"].Values["name"]) + "',AuditDate=getdate() where StockId=" + id);
                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('批量审核成功');", true);
                }
                else
                {
                    n++;
                }
                if (n == this.GvData.Rows.Count)
                {

                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('至少应选择一项')", true);
                }
            }
        }
        zs.Close();
    }
    protected void GvData_RowDataBound(object sender, GridViewRowEventArgs e)//权限设置
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //删除是否可用
            string AuditState = DataBinder.Eval(e.Row.DataItem, "AuditState").ToString();
            if (Convert.ToInt32(AuditState) > 0)
            {
                e.Row.Cells[13].Text = "已通过";
            }else
            {
                e.Row.Cells[13].Text = "未通过";
            }
        }
    }
    protected void Excel_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "GB2312";
        Response.AppendHeader("Content-Disposition", "attachment;filename=StorageManage" + DateTime.Now.Date.ToString("yyyyMMdd") + ".xls");
        // 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码！！！
        Response.ContentEncoding = System.Text.Encoding.UTF7;
        Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
        //  turn off paging 

        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        this.GvData.RenderControl(htw);
        string strHtml = sw.ToString().Trim();
        Response.Output.Write(strHtml);
        Response.Flush();
        Response.End();
        // turn the paging on again 

    }
    //添加一个空方法,防止   GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内错误
    public override void VerifyRenderingInServerForm(Control control)
    {
        // Confirms that an HtmlForm. control is rendered for
    }
}
